// pages/operat/codeManager/codeAddOrEdit/codeAddOrEdit.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeModel: null,
    isEdit: '',
    areaIndex: 0,
    productSelectItem: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isEdit === "false") {
      wx.setNavigationBarTitle({
        title: '详细信息',
      })
    }
    let model = LApp.pages.get("codeManager/codeManager").codeModel;
    if (model) {
      this.setData({
        codeModel: model,
        isEdit: options.isEdit
      })
    } else {
      this.setDate({
        codeModel: {}
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getProductSelectItemList();
    // var interceptor = {
    //   set: function (receiver, property, value) {
    //     console.log(property, 'is changed to', value);
    //     receiver[property] = value;
    //   }
    // };
    // new Proxy(this.data.codeModel, interceptor);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 下拉框获取数据
   */
  getProductSelectItemList: function () {
    let _this = this;
    if (_this.data.isEdit === "false") {
      return;
    }
    LApp._get({
      url: "nbapi/product/selectitem",
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId, typeId: this.data.codeModel.ProductTypeId },
      success: function (r) {
        _this.setData({
          productSelectItem: r
        })
        for (var item in _this.data.productSelectItem) {
          if (_this.data.productSelectItem[item].Code === _this.data.codeModel.ProductId) {
            _this.setData({
              areaIndex: item
            })
            break;
          }
        }
      }
    })
  },
  onSaveClick: function (e) {
    LApp._put({
      url: "nbapi/codemanager/gencode/update",
      data: {
        GenCodeId: this.data.codeModel.GenCodeId,
        EnterpriseUse: this.data.codeModel.EnterpriseUse,
        Query_Result_Content: this.data.codeModel.Query_Result_Content,
        Query_Message_Content: this.data.codeModel.Query_Message_Content,
        BatchId: this.data.codeModel.BatchId,
        ProductId: this.data.codeModel.ProductId,
        CustomerId: this.data.codeModel.CustomerId,
        ProductBrand: this.data.codeModel.ProductBrand
      },
      success: function (r) {
        wx.$$getParent().genCodeList=[];
        wx.$$getParent().getGenCodeList();
        wx.showToast({
          title: "修改完成",
          icon: "success"
        })
        wx.navigateBack({
          delta:1
        })
      }
    })
  },
  /**
   * 下拉框选值
   */
  productNameSelect: function (e) {
    this.setData({
      areaIndex: e.detail.value
    });
    this.data.codeModel.ProductName = this.data.productSelectItem[this.data.areaIndex].Name;
    this.data.codeModel.ProductId = this.data.productSelectItem[this.data.areaIndex].Code;
    this.setData({
      codeModel: this.data.codeModel
    })
  },
  bindModelChange: function (e) {
    wx.$$apply(e, this.data.codeModel);
  },
  pageComeBack: function () {
    wx.navigateBack({
      delta: 1
    })
  }
})